#include "pic.h"

Pic::Pic(QWidget *parent):QWidget(parent)
{
    ready=false;
    this->mask=15; //
    this->toprad=0.001;
    this->omg =0.5; //need many times 0.98
    this->t0=0.1 ; // 0.1
}
void Pic::paintEvent ( QPaintEvent * event ){
    if (ready){
        QPainter g(this);
        QRect box = this->rect();
        g.drawImage(box,this->img);
        g.end();
    }
}

void Pic::setready(bool val){
    this->ready=val;
    this->update();
}

void Pic::setimg(QImage myimg){
    this->img = myimg;
}

void Pic::rmfog(){
    if (this->ready){
        mkdark();
        mklight();
        calc();
    }
}

void Pic::mkdark(){
    int st = this->mask/2; // 15/2=7 [-7,7]
    int hi = img.height();
    int wi = img.width();
    QVector<QVector<int> > bak;
    dark.resize(hi);
    bak.resize(hi);
    int i,j;
    for(i=0;i<hi;i++){
        dark[i].resize(wi);
        bak[i].resize(wi);
    }
    for (i=0;i<hi;i++)
        for (j=0;j<wi;j++){
            QRgb tmp = img.pixel(j,i);
             int red = qRed(tmp);
             int green = qGreen(tmp);
             int blue = qBlue(tmp);
            dark[i][j]=qMin(qMin(red,green),blue);
        }

    int dx,dy;
    for (i=0;i<hi;i++)
        for (j=0;j<wi;j++){
            int best = dark[i][j];
            for (dx=-st;dx<=st;dx++)
                for(dy=-st;dy<=st;dy++){
                    int x = i+dx;
                    int y = j+dy;
                    if (x>=0 && x<hi &&y>=0 &&y<wi){
                        best=qMin(best,dark[x][y]);
                    }
                }
            bak[i][j]=best;
        }
    dark = bak;
    //qDebug()<<dark;
}

void Pic::mklight(){

    int hi = img.height();
    int wi = img.width();
    int sz=int(hi*wi*toprad);
    if (sz<=0)
        sz=qMin(hi*wi,100);
/*
    DarkHeap hp;
    hp.resize(sz);
    int i,j;
    for (i=0;i<hi;i++)
        for(j=0;j<wi;j++){
            QPoint pos(i,j);
            DarkNode nd;
            nd.set(dark[i][j],pos);
            hp.push(nd);
        }
    int best=0;
    for (i=0;i<hp.n;i++){
        QPoint pos = hp.hp[i].pos;
        QRgb tmp = img.pixel(pos.y() , pos.x());
        QColor col(tmp); //rgb 2 hsv
        int v =col.value();
        if (best<v){
            best=v;
            this->light=tmp;
        }
    }
    qDebug()<<"bright: "<<best;
    */
    int i,j;
    int best=0;
    QVector<DarkNode> dat;
    for (i=0;i<hi;i++)
        for(j=0;j<wi;j++){
            QPoint pos(i,j);
            DarkNode nd;
            nd.set(dark[i][j],pos);
            dat.push_back(nd);
        }
    int tot=hi*wi;
    sort(dat.begin(),dat.end());
    for (i=0;i<sz;i++){
        QPoint pos = dat[i].pos;
        QRgb tmp = img.pixel(pos.y() , pos.x());
        QColor col(tmp); //rgb 2 hsv
        //int v =col.value();
        int red = qRed(tmp);
        int green = qGreen(tmp);
        int blue = qBlue(tmp);
        int v = (int)(0.299*red+0.587*green+0.114*blue );
        if (best<v){
            best=v;
            this->light=tmp;
        }
    }
    qDebug()<<"bright: "<<best;
}

void Pic::calc(){
    int hi = img.height();
    int wi = img.width();
    int i,j;
     int Ar = qRed(light);
    int Ag = qGreen(light);
    int Ab = qBlue(light);
    int best =(int)(0.299*Ar+0.587*Ag+0.114*Ab );
    //QColor(light).value();
  Ar=Ag=Ab=best;
   //  Ar=Ag=Ab=mean;
    for (i=0;i<hi;i++)
        for(j=0;j<wi;j++){
             QRgb tmp = img.pixel(j,i);
             int red = qRed(tmp);
             int green = qGreen(tmp);
             int blue = qBlue(tmp);
             double t;

             //red
             t=1 - omg*(dark[i][j]/(double)Ar);
             t=qMax(t,t0);
             int r=int((red-Ar)/t+Ar);
             //green
             t=1-omg*(dark[i][j]/(double)Ag);
             t=qMax(t,t0);
             int g = int((green-Ag)/t+Ag);
             //blue
             t=1-omg*(dark[i][j]/(double)Ab);
             t=qMax(t,t0);
             int b =((blue-Ab)/t+Ab);
             r=fix(r);
             g=fix(g);
             b=fix(b);
             img.setPixel(j,i,qRgb(r,g,b));
        }
    this->update();
}

/* bak
   int Ar = qRed(light);
    int Ag = qGreen(light);
    int Ab = qBlue(light);
    //Ar=Ag=Ab=QColor(light);

    for (i=0;i<hi;i++)
        for(j=0;j<wi;j++){
             QRgb tmp = img.pixel(j,i);
             int red = qRed(tmp);
             int green = qGreen(tmp);
             int blue = qBlue(tmp);
             double t;
             //red
             t=1 - omg*(dark[i][j]/(double)Ar);
             int r=int((red-Ar)/t+Ar);
             //green
             t=1-omg*(dark[i][j]/(double)Ag);
             int g = int((green-Ag)/t+Ag);
             //blue
             t=1-omg*(dark[i][j]/(double)Ab);
             int b =((blue-Ab)/t+Ab);
             img.setPixel(j,i,qRgb(r,g,b));
        }





        int hi = img.height();
    int wi = img.width();
    int i,j;
    int Ar = qRed(light);
    int Ag = qGreen(light);
    int Ab = qBlue(light);
    //Ar=Ag=Ab=QColor(light);
    int A = (Ar+Ag+Ab)/3;
    qDebug()<<"A: "<<A;
    for (i=0;i<hi;i++)
        for(j=0;j<wi;j++){
             QRgb tmp = img.pixel(j,i);
             int red = qRed(tmp);
             int green = qGreen(tmp);
             int blue = qBlue(tmp);
             double t = 1-(omg*dark[i][j]/(double)A);
            // t=qMax(t,t0);
             int aim =int((tmp-light)/t+light);
             img.setPixel(j,i,aim);
        }

*/
